import axios from 'axios'
import Element from 'element-ui'

 //axios.defaults.baseURL='http://localhost:8082';
// axios.defaults.baseURL='http://131.55.42.144:8081 ';


// 请求拦截
axios.interceptors.request.use(config =>{
    console.log("axios的请求拦截器")
    //let token = window.$cookies.get('token') 
    //config.headers.common['Authorization'] = token
    // 可以统一设置请求头
    return config
} );

// 响应拦截
axios.interceptors.response.use(response=>{
    let res = response.data;
    console.log("响应体：" + res.msg);

    if (res.code < 300){
        
        return response;
    } else {
        return Promise.reject(response.data.msg)
    }
});
error => {
    console.log('error：' + error)// for debug
    if(error.response.data) {
        error.message = error.response.data.message
    }
    // 根据请求状态觉得是否登录或者提示其他
    if (error.response.code === 401) {
        store.commit('REMOVE_INFO');
        router.push({
            path: '/login'
        });
        error.message = '请重新登录';
    }
    if (error.response.status === 403) {
        error.message = '权限不足，无法访问';
    }
    Element.Message({
        message: error.message,
        type: 'error',
        duration: 3 * 1000
    })
    return Promise.reject(error)
};
